// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// import axios from 'axios'
import ElementUI from 'element-ui'
import store from './store'
import 'element-ui/lib/theme-chalk/index.css'

// axios.defaults.baseURL = 'http://localhost:8888/api'
Vue.use(ElementUI)
// Vue.prototype.$axios = axios
// Vue.prototype.$store = store
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.auth)) {
    if (to.name === 'login') {
      next()
    } else {
      if (this.token) {
        this.$axios.post('/loginCheck', {
          token: this.token
        })
          .then(response => {
            if (response.data.code === 10) {
              next()
            } else {
              this.$message.info('您的连接已失效,即将重新登录!')
              next('login')
            }
          })
      } else {
        next('login')
      }
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  computed: {
    token: this.$store.token
  }
})
